﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.model.Instance;
using l1cs.server.datatables;

namespace l1cs.server.model
{
    /// <summary>
    /// 血盟クラス
    /// </summary>
    public class L1Clan
    {
        public const int CLAN_RANK_PROBATION = 1;
        public const int CLAN_RANK_PUBLIC = 2;
        public const int CLAN_RANK_GUARDIAN = 3;
        public const int CLAN_RANK_PRINCE = 4;

        private static Logger _log = Logger.getLogger( typeof( L1Clan ).FullName );

        private int _clanId;

        private String _clanName;

        private int _leaderId;

        private String _leaderName;

        private int _castleId;

        private int _houseId;

        private int _warehouse = 0;

        private L1DwarfForClanInventory _dwarfForClan;

        private List<String> membersNameList = new List<String>();

        public L1Clan()
        {
            _dwarfForClan = new L1DwarfForClanInventory( this );
        }

        public int getClanId()
        {
            return _clanId;
        }

        public void setClanId( int clan_id )
        {
            _clanId = clan_id;
        }

        public String getClanName()
        {
            return _clanName;
        }

        public void setClanName( String clan_name )
        {
            _clanName = clan_name;
        }

        public int getLeaderId()
        {
            return _leaderId;
        }

        public void setLeaderId( int leader_id )
        {
            _leaderId = leader_id;
        }

        public String getLeaderName()
        {
            return _leaderName;
        }

        public void setLeaderName( String leader_name )
        {
            _leaderName = leader_name;
        }

        public int getCastleId()
        {
            return _castleId;
        }

        public void setCastleId( int hasCastle )
        {
            _castleId = hasCastle;
        }

        public int getHouseId()
        {
            return _houseId;
        }

        public void setHouseId( int hasHideout )
        {
            _houseId = hasHideout;
        }

        public void addMemberName( String member_name )
        {
            if ( !membersNameList.Contains( member_name ) )
            {
                membersNameList.Add( member_name );
            }
        }

        public void delMemberName( String member_name )
        {
            if ( membersNameList.Contains( member_name ) )
            {
                membersNameList.Remove( member_name );
            }
        }

        public L1PcInstance[] getOnlineClanMember() // オンライン中のクラン員のみ
        {
            List<L1PcInstance> onlineMembers = new List<L1PcInstance>();
            foreach ( String name in membersNameList )
            {
                L1PcInstance pc = L1World.getInstance().getPlayer( name );
                if ( pc != null && !onlineMembers.Contains( pc ) )
                {
                    onlineMembers.Add( pc );
                }
            }
            return onlineMembers.ToArray();
        }

        public String getOnlineMembersFP()
        { 
            // FP means "For Pledge"
            String result = "";
            foreach ( String name in membersNameList )
            {
                L1PcInstance pc = L1World.getInstance().getPlayer( name );
                if ( pc != null )
                {
                    result = result + name + " ";
                }
            }
            return result;
        }

        public String getAllMembersFP()
        {
            String result = "";
            foreach ( String name in membersNameList )
            {
                result = result + name + " ";
            }
            return result;
        }

        public String getOnlineMembersFPWithRank()
        {
            String result = "";
            foreach ( String name in membersNameList )
            {
                L1PcInstance pc = L1World.getInstance().getPlayer( name );
                if ( pc != null )
                {
                    result = result + name + getRankString( pc ) + " ";
                }
            }
            return result;
        }

        public String getAllMembersFPWithRank()
        {
            String result = "";
            foreach ( String name in membersNameList )
            {
                L1PcInstance pc = CharacterTable.getInstance().restoreCharacter( name );
                if ( pc != null )
                {
                    result = result + name + getRankString( pc ) + " ";
                }
            }
            return result;
        }

        private String getRankString( L1PcInstance pc )
        {
            String rank = "";
            if ( pc != null )
            {
                if ( pc.getClanRank() == CLAN_RANK_PROBATION )
                {
                    rank = "[見習い]";
                }
                else if ( pc.getClanRank() == CLAN_RANK_PUBLIC )
                {
                    rank = "[一般]";
                }
                else if ( pc.getClanRank() == CLAN_RANK_GUARDIAN )
                {
                    rank = "[ガーディアン]";
                }
                else if ( pc.getClanRank() == CLAN_RANK_PRINCE )
                {
                    rank = "[血盟君主]";
                }
                else
                {
                    rank = "";
                }
            }
            return rank;
        }

        public String[] getAllMembers()
        {
            return membersNameList.ToArray();
        }

        /// <summary>
        /// ドワーフ倉庫のインスタンスを返す
        /// </summary>
        /// <returns></returns>
        public L1DwarfForClanInventory getDwarfForClanInventory()
        {/*TTOK*/
            return _dwarfForClan;
        }

        public int getWarehouseUsingChar()
        {
            return _warehouse;
        }

        public void setWarehouseUsingChar( int objid )
        {
            _warehouse = objid;
        }
    }
}
